package com.qianji.petmall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianji.common.utils.PageUtils;
import com.qianji.common.utils.Query;
import com.qianji.petmall.coupon.dao.SeckillSessionDao;
import com.qianji.petmall.coupon.entity.SeckillSessionEntity;
import com.qianji.petmall.coupon.entity.SeckillSkuRelationEntity;
import com.qianji.petmall.coupon.service.SeckillSessionService;
import com.qianji.petmall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {

    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> lates3Day() {
//        this.baseMapper.lates3Day();
        //计算最近三天
        //查出这三天参与秒杀活动的商品
        List<SeckillSessionEntity> list = this.baseMapper.selectList(new QueryWrapper<SeckillSessionEntity>().between(
                "start_time", dataStart(), dataEnd()));
        //查询出所有的秒杀活动商品关联信息
        List<SeckillSkuRelationEntity> entityList = seckillSkuRelationService.list();

        if (list!=null && list.size()>0){
            List<SeckillSessionEntity> entity = list.stream().map(item -> {
                List<SeckillSkuRelationEntity> collect = entityList.stream().filter(i -> i.getPromotionSessionId().equals(item.getId())).collect(Collectors.toList());
                item.setSkuRelations(collect);
                return item;
            }).collect(Collectors.toList());
            return entity;
        }
        return null;
    }
    //初始时间
    public String dataStart(){
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        LocalDateTime of = LocalDateTime.of(now, min);

        String format = of.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }
    //三天后的时间
    public String dataEnd(){
        LocalDate now = LocalDate.now();
        LocalDate localDate = now.plusDays(2);
        LocalTime max = LocalTime.MAX;
        LocalDateTime of = LocalDateTime.of(localDate, max);

        String format = of.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

}